import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:hive_flutter/hive_flutter.dart';

class WelcomeHeaderNotifier extends StateNotifier<bool> {
  WelcomeHeaderNotifier() : super(true) {
    _loadVisibilityState();
  }

  Future<void> _loadVisibilityState() async {
    try {
      final box = Hive.box('app');
      final isVisible = box.get('welcomeHeaderVisible', defaultValue: true);
      state = isVisible;
      print('📱 加载欢迎横幅状态: $isVisible');
    } catch (e) {
      print('❌ 加载欢迎横幅状态失败: $e');
      state = true; // 默认显示
    }
  }

  Future<void> setVisible(bool visible) async {
    try {
      final box = Hive.box('app');
      await box.put('welcomeHeaderVisible', visible);
      state = visible;
      print('✅ 欢迎横幅状态已更新: $visible');
    } catch (e) {
      print('❌ 保存欢迎横幅状态失败: $e');
    }
  }

  Future<void> toggle() async {
    await setVisible(!state);
  }

  Future<void> show() async {
    await setVisible(true);
  }

  Future<void> hide() async {
    await setVisible(false);
  }
}

final welcomeHeaderProvider = StateNotifierProvider<WelcomeHeaderNotifier, bool>((ref) {
  return WelcomeHeaderNotifier();
});
